package com.floreantpos.jreports;

import com.floreantpos.model.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class KitchenTicketDataSource extends AbstractReportDataSource {

  public KitchenTicketDataSource() {
    super(new String[]{"itemNo", "itemName", "itemQty"});
  }

  public KitchenTicketDataSource(Ticket ticket) {
    super(new String[]{"itemNo", "itemName", "itemQty"});

    setTicket(ticket);
  }

  public void setTicket(Ticket ticket) {
    ArrayList<Row> rows = new ArrayList<Row>();

    List<TicketItem> ticketItems = ticket.getTicketItems();
    if (ticketItems != null) {
      for (TicketItem ticketItem : ticketItems) {
        int pizzaFlavorQty = 0;
        if ((ticketItem.isShouldPrintToKitchen() && !ticketItem.isPrintedToKitchen()) || (ticket.isVoided() && ticketItem.isShouldPrintToKitchen())) {
          Row row1 = new Row(ticketItem.getItemCount(), ticketItem.getName(), ticketItem.getItemId());
          rows.add(row1);
        }
        ticketItem.setPrintedToKitchen(true);

        List<TicketItemModifierGroup> modifierGroups = ticketItem.getTicketItemModifierGroups();
        if (modifierGroups != null) {
          for (TicketItemModifierGroup modifierGroup : modifierGroups) {
            List<TicketItemModifier> modifiers = modifierGroup.getTicketItemModifiers();
            if (modifiers != null) {
              for (TicketItemModifier modifier : modifiers) {
                if (modifier.isPizzaModifier()) {
                  pizzaFlavorQty += 1;
                }
              }
            }
          }
          for (TicketItemModifierGroup modifierGroup : modifierGroups) {
            List<TicketItemModifier> modifiers = modifierGroup.getTicketItemModifiers();
            if (modifiers != null) {
              for (TicketItemModifier modifier : modifiers) {
                if (!ticketItem.isShouldPrintToKitchen() || !modifier.isShouldPrintToKitchen() || modifier.isPrintedToKitchen()) {
                  continue;
                }
                modifier.setPrintedToKitchen(true);

                String modifierName = "";
                int modifierItemCount = 0;

                if (modifier.getModifierType() == TicketItemModifier.NO_MODIFIER) {
                  modifierName = "  - " + modifier.getName();
                }

                if (modifier.getModifierType() == TicketItemModifier.NORMAL_MODIFIER) {
                  modifierName = "  + " + modifier.getName();
                  modifierItemCount = modifier.getItemCount();
                }

                if (modifier.getModifierType() == TicketItemModifier.EXTRA_MODIFIER) {
                  modifierName = "  ++ " + modifier.getName();
                  modifierItemCount = modifier.getItemCount();
                }
                Row row = new Row();
                if (modifier.isPizzaModifier() || modifierItemCount == 0) {
                  if (pizzaFlavorQty > 1) {
                    row.setItemName("  " + modifier.getName() + " (1/" + pizzaFlavorQty + ")");
                  } else {
                    row.setItemName("  " + modifier.getName() + " (" + pizzaFlavorQty + ")");
                  }
                } else {
                  row.setItemName(modifierName + " (" + modifierItemCount + ")");
                }
                row.setItemCount(0);
                row.setItemNo(0);
                rows.add(row);
              }
            }
          }
        }

      }

      Set<TicketCookingInstruction> cookingInstructions = ticket.getCookingInstructions();
      if (cookingInstructions != null) {
        for (Iterator<TicketCookingInstruction> it = cookingInstructions.iterator(); it.hasNext();) {
          TicketCookingInstruction cookingInstruction = it.next();

          if (!cookingInstruction.isPrintedToKitchen()) {
            Row row1 = new Row(-1, cookingInstruction.getDescription(), -1);
            rows.add(row1);
          }
          cookingInstruction.setPrintedToKitchen(true);
        }
      }
    }

    setRows(rows);
  }

  public Object getValueAt(int rowIndex, int columnIndex) {
    Row item = (Row) rows.get(rowIndex);

    switch (columnIndex) {
      case 0:
        return String.valueOf(item.getItemNo());

      case 1:
        return item.getItemName();

      case 2:
        return String.valueOf(item.getItemCount());
    }

    return null;
  }

  private class Row {

    private int itemCount;
    private String itemName;
    private int itemNo;

    public Row() {
      super();
    }

    public Row(int itemCount, String itemName, int itemNo) {
      super();
      this.itemCount = itemCount;
      this.itemName = itemName;
      this.itemNo = itemNo;
    }

    public int getItemCount() {
      return itemCount;
    }

    public void setItemCount(int itemCount) {
      this.itemCount = itemCount;
    }

    public String getItemName() {
      return itemName;
    }

    public void setItemName(String itemName) {
      this.itemName = itemName;
    }

    public int getItemNo() {
      return itemNo;
    }

    public void setItemNo(int itemNo) {
      this.itemNo = itemNo;
    }

  }
}
